package com.mbl.phone.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mbl.phone.pojo.Changjia;
import com.mbl.phone.pojo.User;
import com.mbl.phone.service.DataService;
import com.mbl.phone.utility.ResultUtil;

import net.sf.json.JSONObject;



@Controller
@RequestMapping({"/data/"})
public class DataController
{
	static final Logger logger = LoggerFactory.getLogger(DataController.class);

	@Resource
	DataService dataServer;
	@Resource
	ResultUtil reSult;

	@ResponseBody
	@RequestMapping(value={"changjiaList/{genre}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
	public JSONObject getChangJiaTen(@PathVariable("genre") Integer genre)
	{
		User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
		if (user == null) {
			return this.reSult.UNLOGIN();
		}
		List<Changjia> list = this.dataServer.findTenData(genre);

		this.reSult.SUCCESS(list);

		return this.reSult.getInstance();
	}


	@ResponseBody
	@RequestMapping(value={"changjia/{dataId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
	public JSONObject getChangJia(@PathVariable("dataId") Long dataId)
	{
		User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
		if (user == null) {
			return this.reSult.UNLOGIN();
		}

		Changjia changjia = this.dataServer.findChangjiaById(dataId);

		if (changjia != null) { return this.reSult.SUCCESS(changjia);
		}
		return this.reSult.C_ERROR().getInstance();
	}

	@ResponseBody
	@RequestMapping(value={"changjia/{dataId}"}, method={org.springframework.web.bind.annotation.RequestMethod.PUT})
	public JSONObject modifyChangJia(@PathVariable("dataId") Long dataId, Changjia changjia)
	{
		User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
		if (user == null) {
			return this.reSult.UNLOGIN();
		}

		if (dataId.longValue() == changjia.getId()) {
			return this.reSult.SUCCESS(this.dataServer.updataChangjia(dataId, changjia, user.getId()));
		}
		return this.reSult.C_ERROR().getInstance();
	}



	@ResponseBody
	@RequestMapping(value={"changjia/{dataId}"}, method={org.springframework.web.bind.annotation.RequestMethod.DELETE})
	public JSONObject modifyChangJiaAddBackList(@PathVariable Long dataId)
	{
		User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
		if (user == null) {
			return this.reSult.UNLOGIN();
		}

		Integer updataChangJiaAddBackList = this.dataServer.updataChangJiaAddBackList(dataId, user.getId());

		return this.reSult.SUCCESS(updataChangJiaAddBackList);
	}

	@ResponseBody
	@RequestMapping(value={"changjia/{dataId}"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
	public JSONObject modifyChangJiaMarkHigh(@PathVariable Long dataId)
	{
		User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
		if (user == null) {
			return this.reSult.UNLOGIN();
		}

		Integer updataChangJiaAddBackList = this.dataServer.updataChangJiaMarkHigh(dataId, user.getId());

		return this.reSult.SUCCESS(updataChangJiaAddBackList);
	}











	@ResponseBody
	@RequestMapping(value={"changjiaSign/{sign}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
	public JSONObject getChangJiaSignTen(@PathVariable int sign, @RequestParam("item") int endItem)
	{
		User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
		if (user == null) {
			return this.reSult.UNLOGIN();
		}

		List<Changjia> findChangJiaSignTen = this.dataServer.findChangJiaSignTen(sign, endItem, user);
		if (findChangJiaSignTen.size() == 0) {
			return this.reSult.WARN().getInstance();
		}
		return this.reSult.SUCCESS(findChangJiaSignTen);
	}






	@ResponseBody
	@RequestMapping(value={"changjiaDelete/{dataId}"}, method={org.springframework.web.bind.annotation.RequestMethod.DELETE})
	public JSONObject alwaysDeleteChangjia(@PathVariable int dataId)
	{
		User user = (User)SecurityUtils.getSubject().getSession().getAttribute("user");
		if (user == null) {
			return this.reSult.UNLOGIN();
		}
		Integer isTrue = this.dataServer.deleteChangjiaAlways(dataId, user);

		return this.reSult.SUCCESS(isTrue);
	}
}


